package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.AjaxResponse;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.CourseEva;
import com.zretc.emptraining.entity.Teach;
import com.zretc.emptraining.entity.TeacherEva;
import com.zretc.emptraining.service.TeacherEvaService;
import com.zretc.emptraining.serviceimpl.TeacherEvaServiceImpl;

/**
 * Servlet implementation class TeacherEvaServlet
 */
@WebServlet("/TeacherEvaServlet")
public class TeacherEvaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 创建对象
	TeacherEvaService teacherEvaService = new TeacherEvaServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TeacherEvaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("gbk");
		// 判断请求内容
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("".equals(op)) {
			// 转发
			request.getRequestDispatcher("CourseEvaList.jsp").forward(request, response);
		} else if ("add".equals(op)) {
			addTeacherEva(request, response);
		} else if ("queryByPage".equals(op)) {
			showTeachEva(request, response);
		} else if ("doTeacherEva".equals(op)) {
			doTeacherEva(request, response);
		} else if ("teacherInfo".equals(op)) {
			teacherInfo(request, response);
		} else if ("teacherInfo2".equals(op)) {
			teacherInfo2(request, response);
		}else if ("teacherInfo3".equals(op)) {
			teacherInfo3(request, response);
		}
	}
	private void teacherInfo3(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 8;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<Teach> pd = teacherEvaService.getTeacherEvaShow(page, pageSize);

		// 存储值
		request.setAttribute("pd", pd);

		// 转发
		request.getRequestDispatcher("TeacherShow3.jsp").forward(request, response);
	}
	private void teacherInfo2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 8;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<Teach> pd = teacherEvaService.getTeacherEvaShow(page, pageSize);

		// 存储值
		request.setAttribute("pd", pd);

		// 转发
		request.getRequestDispatcher("TeacherShow2.jsp").forward(request, response);
	}

	private void teacherInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 8;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<Teach> pd = teacherEvaService.getTeacherEvaShow(page, pageSize);

		// 存储值
		request.setAttribute("pd", pd);
		
		// 转发
		request.getRequestDispatcher("TeacherShow.jsp").forward(request, response);
	}

	private void doTeacherEva(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// ajax判断是否第一次评价
		response.setContentType("application/json;charset=utf-8");
		// 调用添加教员评价的方法
		// 获取文本框里的值
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));

		// 添加课程
		List<TeacherEva> list = teacherEvaService.doTeacherEva(empNo, cpNo);
		// 标志
		boolean flag = false;// 不是第一次提交
		TeacherEva msg = null;
		if (list.size() == 0) {
			flag = true;// 是第一次提交
		} else {
			msg = list.get(0);
		}

		AjaxResponse msgs = null;
		// 创建gson
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		// 判断是否有这个分数
		if (msg != null) {
			msgs = new AjaxResponse(Double.toString(msg.getEvalScore()), flag);// 存储信息
			String jsonString = gson.toJson(msgs);
			// 打印
			out.print(jsonString);
		} else {
			msgs = new AjaxResponse(Double.toString(0), flag);// 存储信息
			String jsonString = gson.toJson(msgs);
			// 打印
			out.print(jsonString);
		}
		// 关闭
		out.close();
	}

	private void showTeachEva(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容
		String empNoQuery = "";
		if (request.getParameter("empNoQuery") != null) {
			empNoQuery = request.getParameter("empNoQuery");
		}
		System.out.println(empNoQuery);
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 7;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<TeacherEva> pd = null;
		// 下拉框选择
		String selectKey = "教员名";
		if (request.getParameter("selectKey") != null) {
			selectKey = request.getParameter("selectKey");
		}
		if ("".equals(empNoQuery)) {
			pd = teacherEvaService.getTeacherEvaByCourseName(page, pageSize, empNoQuery);
		} else if ("教员名".equals(selectKey)) {
			pd = teacherEvaService.getTeacherEvaByTeacherName(page, pageSize, empNoQuery);
		} else if ("课程名".equals(selectKey)) {
			pd = teacherEvaService.getTeacherEvaByCourseName(page, pageSize, empNoQuery);
		}
		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("empNoQuery", empNoQuery);
		request.setAttribute("selectKey", selectKey);
		// 转发
		request.getRequestDispatcher("TeacherEvaList.jsp").forward(request, response);
	}

	private void addTeacherEva(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		// 调用添加教员评价的方法
		// 获取文本框里的值
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		int teacherNo = Integer.parseInt(request.getParameter("teacherNo"));
		int evalScore = Integer.parseInt(request.getParameter("evalScore"));
		TeacherEva teacherEva = new TeacherEva(0, empNo, cpNo, teacherNo, evalScore);
		// 添加课程
		boolean flag = teacherEvaService.addTeacherEva(teacherEva);
		// 创建gson
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter out = response.getWriter();
		// 打印
		out.print(jsonString);
		// 关闭
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
